package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;


    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {


        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> elqw = new LambdaQueryWrapper<>();
        elqw.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(elqw);
        if (one == null) {
            return R.error("登陆失败,用户不存在");
        }

        if (!one.getPassword().equals(password)) {
            return R.error("密码错误");
        }
        if (one.getStatus() == 0) {
            return R.error("账户已被禁用");
        }
        request.getSession().setAttribute("employee", one.getId());

        return R.success(one);
    }


    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("您已退出成功");
    }

    //添加员工
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee) {
        log.info("新增员工，员工信息：{}",employee.toString());
        //设置密码并使用MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//
//        //获取当前用户登录的id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        //封装
        employeeService.save(employee);
        return R.success("添加员工成功");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        log.info("page = {}, pageSize = {}, name = {}",page,pageSize,name);

        //创建分页构造器
        Page pageinfo = new Page(page,pageSize);

        //模糊查询
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper();
        //添加条件
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //排序条件
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageinfo,lambdaQueryWrapper);
        return R.success(pageinfo);
    }

    //修改员工信息
    @PutMapping
    public R update( HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());

        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);

        //Long empId = (Long) request.getSession().getAttribute("employee");

        //获取当前更改时间
        //employee.setUpdateTime(LocalDateTime.now());
        //获取当前修改的人的name
        //employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }


    //根据id查询员工信息
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("根据员工id进行查询信息");
        Employee byId = employeeService.getById(id);
        if (byId !=null){
            return R.success(byId);

        }
        return R.error("没有查询到该员工信息");
    }
}
